package com.example.courseservice.service.impl;

import com.example.courseservice.entity.*;
import com.example.courseservice.repository.*;
import com.example.courseservice.service.CourseService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.NoSuchElementException;

@Service
public class CourseServiceImpl implements CourseService {

    private final CourseRepository courseRepository;
    private final ChapterRepository chapterRepository;
    private final MaterialRepository materialRepository;
    
    public CourseServiceImpl(CourseRepository courseRepository, 
                           ChapterRepository chapterRepository,
                           MaterialRepository materialRepository) {
        this.courseRepository = courseRepository;
        this.chapterRepository = chapterRepository;
        this.materialRepository = materialRepository;
    }

    @Override
    @Transactional
    public Course createCourse(Course course) {
        return courseRepository.save(course);
    }

    @Override
    @Transactional
    public Course updateCourse(String courseId, Course course) {
        Course existingCourse = courseRepository.findById(courseId)
                .orElseThrow(() -> new NoSuchElementException("Course not found with ID: " + courseId));
        
        existingCourse.setCourseName(course.getCourseName());
        existingCourse.setCourse(course.getCourse());
        existingCourse.setTeachingGoal(course.getTeachingGoal());
        existingCourse.setExaminationMethod(course.getExaminationMethod());
        
        return courseRepository.save(existingCourse);
    }

    @Override
    @Transactional
    public void deleteCourse(String courseId) {
        if (!courseRepository.existsById(courseId)) {
            throw new NoSuchElementException("Course not found with ID: " + courseId);
        }
        courseRepository.deleteById(courseId);
    }

    @Override
    @Transactional(readOnly = true)
    public Course getCourseById(String courseId) {
        return courseRepository.findById(courseId)
                .orElseThrow(() -> new NoSuchElementException("Course not found with ID: " + courseId));
    }

    @Override
    @Transactional(readOnly = true)
    public List<Course> getAllCourses() {
        return courseRepository.findAll();
    }

    @Override
    @Transactional
    public void addMaterial(String courseId, String chapterId, Material material) {
        materialRepository.save(material);
    }

    @Override
    @Transactional
    public void deleteMaterial(String courseId, String chapterId, String materialId) {
        materialRepository.deleteById(materialId);
    }

    @Override
    @Transactional
    public void addChapter(String courseId, Chapter chapter) {
        chapterRepository.save(chapter);
    }

    @Override
    @Transactional
    public void deleteChapter(String courseId, String chapterId) {
        chapterRepository.deleteById(chapterId);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Chapter> getChapters(String courseId) {
        return chapterRepository.findAll();
    }
}